#!/usr/bin/perl -w
use strict;

use FindBin;
use lib ("$FindBin::Bin/..", "/net/cpp-group/Leo/bin");
use db_parameters;
use ortho_species;
use calculate_ortholog_type_in_ortholog_sets;
use DBI;


print STDERR <<"HEADLINE";
888888888888888888888888888888888888888888888888888888888888888888888

    table_ortholog_sets

	Description:
		Enter Ortholog sets into panda
	Populates DB tables:
		orthologs.ortholog_sets
	Relies on:
		ens_id
		(especially after phyop orthologs resolved)

888888888888888888888888888888888888888888888888888888888888888888888

HEADLINE

#-----------------------------------------------------------------------------------------
#	Get DB parameters
#
use db_parameters;
#
#	Get DB parameters
#-----------------------------------------------------------------------------------------



#
# connect to genome database
#
my $dbh = connect_to_panda();
use get_psql_time;
my $start_time = get_psql_time($dbh);



#-----------------------------------------------------------------------------------------
#
#	load_ortholog_set_to_panda
#
#
sub load_ortholog_set_to_panda($$)
{
	my ($dbh, $file_name) = @_;
	my $ortholog_id = 0;
	open ORTHODATA, $file_name or die "Error\n\tCould not open $file_name\n$@";
	my $cnt_lines = 0;
	$dbh->pg_putline($_) while (<ORTHODATA>);
}



	#
	#	Load ortholog sets into panda
	#
	my $species12 = $ortho_name1. '_'.$ortho_name2;
	my $dir_pipeline_temp_orthologs	= "$dir_pipeline_ds_temp/$species12/orthologs";
	my $ambiguous_file_name		= "$dir_orthologs_output/resolve_orthologs/clades/orthologs_resolve.ambiguous.clades";
	my $unambiguous_file_name	= "$dir_orthologs_output/resolve_orthologs/clades/orthologs_resolve.unambiguous.clades";
	my $singlechoice_file_name	= "$dir_orthologs_output/resolve_orthologs/clades/orthologs_resolve.single_choice.clades";
	
	print STDERR "\tEntering into panda...\n";
	$dbh->do("DELETE FROM orthologs.ortholog_sets WHERE protocol_id = $curr_protocol_id;")
								or die $dbh->errstr.".";
	$dbh->do("COPY orthologs.ortholog_sets(".
					"ortholog_id, ds, prot_id) FROM STDIN") or die $dbh->errstr.".";
	for my $file_name ($singlechoice_file_name, $unambiguous_file_name, $ambiguous_file_name)
	{
		load_ortholog_set_to_panda($dbh, $file_name)
	}
	$dbh->pg_putline("\\.\n");
	$dbh->pg_endcopy or die $dbh->errstr.".";



	#
	#	Update ortholog_sets with gene_id and species (as well as protocol_id)
	#
	print STDERR "\tUpdate ortholog_sets with gene_id and species (as well as protocol_id)\n";
	$dbh->do( <<"PL/SQLCMD" ) or die $dbh->errstr.".";
    UPDATE
            orthologs.ortholog_sets 
        SET
            gene_id = ens_id.gene_id,
            disentangled_gene_id = ens_id.gene_id,
            protocol_id = $curr_protocol_id,
            species = ens_id.species
        FROM 
            ens_id 
        WHERE 
            orthologs.ortholog_sets.prot_id = ens_id.prot_id AND
            protocol_id IS NULL;
PL/SQLCMD







	#
	#	Update ortholog_sets with split genes
	#
	print STDERR "\tUpdate ortholog_sets with split genes\n";
	$dbh->do( <<"PL/SQLCMD" ) or die $dbh->errstr.".";
    UPDATE
            orthologs.ortholog_sets 
        SET
            disentangled_gene_id = gene_id || '&' || prot_id
        WHERE 
            gene_id IN 
            (
                SELECT 
                        gene_id 
                    FROM
                        orthologs.ortholog_sets 
                    WHERE 
                        protocol_id = $curr_protocol_id
                    GROUP BY 
                        gene_id 
                    HAVING COUNT(*) > 1
            ) AND
            protocol_id = $curr_protocol_id
PL/SQLCMD


calculate_ortholog_type_in_ortholog_sets($dbh, $curr_protocol_id, 1);




log_pipeline_stage($dbh, $curr_protocol_id, 33, 'table ortholog_sets', $start_time);
$dbh->disconnect();

